//
//  CZLProgressView.m
//  test
//
//  Created by 程智龙 on 16/1/12.
//  Copyright © 2016年 码农君－lorne. All rights reserved.
//

#import "CZLProgressView.h"
#define degreesToRadians(x) (M_PI*(x)/180.0)
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
@interface CZLProgressView()
@property (nonatomic,assign) CGFloat thicknessRatio ;//环形厚度0.0～1.0;
@property(nonatomic,assign) CGFloat thicknessouter;//最外边框宽1.0+;
@property (nonatomic, strong) UIColor *innerBackgroundColor;//内环颜色
@property (nonatomic, strong) UIColor *outerBackgroundColor;//外环
@property (nonatomic, strong) UIColor *progressTopGradientColor;//渐变色 
@property (nonatomic, strong) UIColor *progressBottomGradientColor;//渐变色


@property(nonatomic,strong)CAShapeLayer *trackLayer;
@property(nonatomic,strong) CAGradientLayer *gradientLayer;

@end
@implementation CZLProgressView

- (id)init
{
    
    self = [super initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 200.0f)];
    if (self) {
        [self setInnerBackgroundColor:[UIColor colorWithRed:228/255.0 green:221/255.0 blue:215/255.0 alpha:1]];
        [self setOuterBackgroundColor:[UIColor colorWithRed:183/255.0 green:177/255.0 blue:171/255.0 alpha:1]];
        [self setProgressTopGradientColor:[UIColor colorWithRed:136/255.0 green:215/255.0 blue:192/255.0 alpha:1]];
        [self setProgressBottomGradientColor:[UIColor colorWithRed:167/255.0 green:192/255.0 blue:94/255.0 alpha:1]];
        [self setThicknessRatio:0.31f];
        [self setThicknessouter:10.0f];
        
        [self setBackgroundColor:[UIColor clearColor]];
        
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0,0)radius:200/2.0-self.thicknessouter/2.0 startAngle:degreesToRadians(0)endAngle:degreesToRadians(360) clockwise:YES];//
        
       CAShapeLayer *  trackLayer=[CAShapeLayer layer];
        trackLayer.frame=CGRectMake(100, 100, 200, 200);
        [self.layer addSublayer:trackLayer];
        trackLayer.fillColor=[UIColor clearColor].CGColor;
        trackLayer.strokeColor=self.outerBackgroundColor.CGColor;
        trackLayer.lineCap=kCALineCapRound;
        trackLayer.lineJoin=kCALineCapRound;
        trackLayer.lineWidth=self.thicknessouter+1;
        trackLayer.path=[path CGPath];
        
        
        CGFloat circleWidth = (200/2.0-self.thicknessouter) * _thicknessRatio;
        UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0,0)radius:200/2.0-self.thicknessouter-circleWidth+1 startAngle:degreesToRadians(0)endAngle:degreesToRadians(360) clockwise:YES];//
        
        CAShapeLayer *  trackLayer2=[CAShapeLayer layer];
        trackLayer2.frame=CGRectMake(100, 100, 200, 200);
        [self.layer addSublayer:trackLayer2];
        trackLayer2.fillColor=self.innerBackgroundColor.CGColor;
        trackLayer2.strokeColor=[UIColor clearColor].CGColor;
        trackLayer2.lineCap=kCALineCapRound;
        trackLayer2.lineJoin=kCALineCapRound;
        trackLayer2.lineWidth=0.0;
        trackLayer2.path=[path2 CGPath];
        
        


    }
   
   
    return self;
}
- (void)drawRect:(CGRect)rect
{
   
    
    // Calculate position of the circle
    CGFloat progressAngle = _CZLprogress * 360.0 - 90;
    CGPoint center = CGPointMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
    CGFloat radius = MIN(rect.size.width, rect.size.height) / 2.0f-_thicknessouter;
    
    CGRect square;
    if (rect.size.width > rect.size.height)
    {
        square = CGRectMake((rect.size.width - rect.size.height) / 2.0, 0.0, rect.size.height-_thicknessouter, rect.size.height-_thicknessouter);
    }
    else
    {
        square = CGRectMake(0.0, (rect.size.height - rect.size.width) / 2.0, rect.size.width-_thicknessouter, rect.size.width-_thicknessouter);
    }
    CGFloat circleWidth = radius * _thicknessRatio;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
   
    
    
    
    // 环的颜色
    CGFloat locations[3] = { 0.0f,1.0f };
    NSArray *gradientColors = @[
                                (id)[[UIColor colorWithRed:92/255.0 green:87/255.0 blue:82/255.0 alpha:1] CGColor],
                                (id)[[UIColor colorWithRed:92/255.0 green:87/255.0 blue:82/255.0 alpha:1] CGColor],
                                ];
    
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)gradientColors, locations);
    CGContextDrawRadialGradient(context, gradient, center, radius - circleWidth, center, radius, 0);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(rgb);
    
    //
    CGFloat locations3[2] = { 0.0f,1.0f };
    NSArray *gradientColors3 = @[
                                 (id)_outerBackgroundColor.CGColor,
                                 (id)_outerBackgroundColor.CGColor,
                                 ];
    
    CGColorSpaceRef rgb3 = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient3 = CGGradientCreateWithColors(rgb3, (__bridge CFArrayRef)gradientColors3, locations3);
    CGContextDrawRadialGradient(context, gradient3, center, radius+_thicknessouter, center, radius, 0);
    CGGradientRelease(gradient3);
    CGColorSpaceRelease(rgb3);
    
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path appendPath:[UIBezierPath bezierPathWithArcCenter:center
                                                    radius:radius
                                                startAngle:DEGREES_TO_RADIANS(-90)
                                                  endAngle:DEGREES_TO_RADIANS(progressAngle)
                                                 clockwise:YES]];
    
    
    [path addArcWithCenter:center
                    radius:radius-circleWidth
                startAngle:DEGREES_TO_RADIANS(progressAngle)
                  endAngle:DEGREES_TO_RADIANS(-90)
                 clockwise:NO];
    
    
    
    [path closePath];
    
    [path addClip];
    
    //变动框
    NSArray *backgroundColors = @[
                                  (id)[_progressTopGradientColor CGColor],
                                  (id)[_progressBottomGradientColor CGColor],
                                  ];
    CGFloat backgroudColorLocations[2] = { 0.0f, 1.0f };
    CGColorSpaceRef rgb2 = CGColorSpaceCreateDeviceRGB();
    CGGradientRef backgroundGradient = CGGradientCreateWithColors(rgb2, (__bridge CFArrayRef)(backgroundColors), backgroudColorLocations);
    CGContextDrawLinearGradient(context,
                                backgroundGradient,
                                CGPointMake(0.0f, square.origin.y),
                                CGPointMake(0.0f, square.size.height),
                                0);
    CGGradientRelease(backgroundGradient);
    CGColorSpaceRelease(rgb2);
    
    
    CGContextRestoreGState(context);
    
    
   
    
    
}
- (void)setCZLprogress:(double)CZLprogress
{
    _CZLprogress=CZLprogress;
    
    [self setNeedsDisplay];
    
}



@end
